﻿@model ProductDetailsModel.AddToCartModel
@using Nop.Core.Domain.Orders
@if (Model.UpdatedShoppingCartItemId > 0)
{
    <input asp-for="UpdatedShoppingCartItemId" type="hidden" />
}
@if (!Model.DisableBuyButton || Model.CustomerEntersPrice)
{
    <div class="add-to-cart">
        @if (Model.CustomerEntersPrice)
        {
            <div class="customer-entered-price">
                <div class="price-input">
                    <label asp-for="CustomerEnteredPrice" asp-postfix=":" class="enter-price-label"></label>
                    @*round price*@
                    <input asp-for="CustomerEnteredPrice" value="@Convert.ToInt32(Math.Ceiling(Model.CustomerEnteredPrice))" class="enter-price-input" />
                </div>
                <div class="price-range">
                    @Model.CustomerEnteredPriceRange
                </div>
            </div>
        }
        @if (!string.IsNullOrEmpty(Model.MinimumQuantityNotification))
        {
            <div class="min-qty-notification">@Model.MinimumQuantityNotification</div>
        }
        @if (!Model.DisableBuyButton)
        {
            <div class="add-to-cart-panel">
                <label asp-for="EnteredQuantity" asp-postfix=":" class="qty-label"></label>
                @if (Model.AllowedQuantities.Count > 0)
                {
                    <select asp-for="EnteredQuantity" asp-items="Model.AllowedQuantities" id="product_enteredQuantity_@Model.ProductId" class="qty-dropdown" aria-label=@T("Products.Qty.AriaLabel")></select>
                    <script asp-location="Footer">
                        $(document).ready(function () {
                            $("#product_enteredQuantity_@Model.ProductId").on("change", function () {
                                var data = {
                                    productId: @Model.ProductId,
                                    quantity: $('#product_enteredQuantity_@Model.ProductId').find(":selected").text()
                                };
                                $(document).trigger({ type: "product_quantity_changed", changedData: data });
                            });
                        });
                    </script>
                }
                else
                {
                    <input asp-for="EnteredQuantity" id="product_enteredQuantity_@Model.ProductId" class="qty-input" type="text" aria-label=@T("Products.Qty.AriaLabel")/>
                    <script asp-location="Footer">
                        //when a customer clicks 'Enter' button we submit the "add to cart" button (if visible)
                        $(document).ready(function() {
                            $("#@Html.IdFor(model => model.EnteredQuantity)").on("keydown", function(event) {
                                if (event.keyCode == 13) {
                                    $("#add-to-cart-button-@Model.ProductId").trigger("click");
                                    return false;
                                }
                            });

                            $("#product_enteredQuantity_@Model.ProductId").on("input propertychange paste", function () {
                                var data = {
                                    productId: @Model.ProductId,
                                    quantity: $('#product_enteredQuantity_@Model.ProductId').val()
                                };
                                $(document).trigger({ type: "product_quantity_changed", changedData: data });
                            });
                        });
                    </script>
                }
                @{
                    var addToCartText = "";
                    if (Model.UpdatedShoppingCartItemId > 0 && Model.UpdateShoppingCartItemType.HasValue && Model.UpdateShoppingCartItemType.Value == ShoppingCartType.ShoppingCart)
                    {
                        addToCartText = T("ShoppingCart.AddToCart.Update").Text;
                    }
                    else
                    {
                        addToCartText = T("ShoppingCart.AddToCart").Text;
                        if (Model.IsRental)
                        {
                            addToCartText = T("ShoppingCart.Rent").Text;
                        }
                        if (Model.AvailableForPreOrder)
                        {
                            addToCartText = T("ShoppingCart.PreOrder").Text;
                        }
                    }
                    <button type="button" id="add-to-cart-button-@Model.ProductId" class="button-1 add-to-cart-button" data-productid="@Model.ProductId" onclick="AjaxCart.addproducttocart_details('@Url.RouteUrl("AddProductToCart-Details", new {productId = Model.ProductId, shoppingCartTypeId = (int) ShoppingCartType.ShoppingCart})', '#product-details-form');return false;">@addToCartText</button>
                }
            </div>
            if (!string.IsNullOrEmpty(Model.PreOrderAvailabilityStartDateTimeUserTime))
            {
                <div class="pre-order-availability-date">
                    <label>@T("ShoppingCart.PreOrderAvailability"):</label>
                    @Html.Raw(Model.PreOrderAvailabilityStartDateTimeUserTime)
                </div>
            }
        }
        @await Component.InvokeAsync("Widget", new { widgetZone = PublicWidgetZones.ProductDetailsAddInfo, additionalData = Model })
    </div>
}